package protobuf.netty.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import protobuf.entity.DataInfo;

import java.util.Random;

public class TestClientHandler extends SimpleChannelInboundHandler<DataInfo.MyMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DataInfo.MyMessage msg) throws Exception {

    }
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        int type = new Random().nextInt(3);//随机生成一个0~2的整数

        DataInfo.MyMessage message;

        if(0 == type){
            message = DataInfo.MyMessage.newBuilder().setDataType(DataInfo.MyMessage.dataType.StudentType)
                    .setStudent(DataInfo.Student.newBuilder()
                            .setName("学生")
                            .setAddress("chongqing")
                            .setAge(18).build())
                    .build();
        }else if(1 == type){
            message = DataInfo.MyMessage.newBuilder().setDataType(DataInfo.MyMessage.dataType.TeacherType)
                    .setTeacher(DataInfo.Teacher.newBuilder()
                            .setName("老师")
                            .setAddress("chongqing")
                            .setAge(22).build())
                    .build();
        }else {
            message = DataInfo.MyMessage.newBuilder().setDataType(DataInfo.MyMessage.dataType.DoctorType)
                    .setDoctor(DataInfo.Doctor.newBuilder()
                            .setName("医生")
                            .setAddress("chongqing")
                            .setAge(30).build())
                    .build();
        }

        ctx.channel().writeAndFlush(message);
    }
}
